package members;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MemberSelect extends HttpServlet {
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Member> item = new ArrayList<Member>();
		
		String InputText= req.getParameter("MID");
		if (InputText != null){	
		try {
			Class.forName("org.h2.Driver");
			Connection con = DriverManager.getConnection("jdbc:h2:~/retail","sa", "");
			PreparedStatement ps = con.prepareStatement("SELECT * FROM MEMBER Where MEMBERID=?");
		    ps.setString(1, InputText);
			ResultSet rs = ps.executeQuery();				
				
			
			while (rs.next()) {
				Member person = new Member();
				person.setMEMBERID(rs.getString("MEMBERID"));
				person.setMEMBERNAME(rs.getString("MEMBERNAME"));
				person.setMEMBERSCORE(rs.getInt("MEMBERSCORE"));
				item.add(person);
			} 
			
			ps.close();
			con.close();

			
		}catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
		
	req.setAttribute("b", item);
	String action = req.getQueryString();
	if(action.equals("act=query")){
		req.getRequestDispatcher("Member/MemberQuery.jsp").forward(req, resp);		
	}		
	if(action.equals("act=delete")){
		req.getRequestDispatcher("Member/MemberDelete.jsp").forward(req, resp);
	}		
	if(action.equals("act=update")){
		req.getRequestDispatcher("Member/MemberUpdate.jsp").forward(req, resp);
	}
	

	}

	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Member> item = new ArrayList<Member>();
		
		String InputText= req.getParameter("MID");
	
		if (InputText != null){	
		try {
			Class.forName("org.h2.Driver");
			Connection con = DriverManager.getConnection("jdbc:h2:~/retail","sa", "");
			PreparedStatement ps = con.prepareStatement("SELECT * FROM MEMBER Where MEMBERID=?");
		    ps.setString(1, InputText);
			ResultSet rs = ps.executeQuery();
			
		//	System.out.print(rs.next());
			//System.out.print(rs);
			
						
			while (rs.next()) {
				Member person = new Member();				
				person.setMEMBERID(rs.getString("MEMBERID"));
				person.setMEMBERNAME(rs.getString("MEMBERNAME"));
				person.setMEMBERSCORE(rs.getInt("MEMBERSCORE"));
				item.add(person);
		} 
			
			ps.close();
			con.close();
			
			
			
		}catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		}
		req.setAttribute("b", item);
		String action = req.getQueryString();
		if(action.equals("act=query")){
			req.getRequestDispatcher("Member/MemberQuery.jsp").forward(req, resp);		
		}
		if(action.equals("act=delete")){
			req.getRequestDispatcher("Member/MemberDelete.jsp").forward(req, resp);
		}
		if(action.equals("act=update")){
			req.getRequestDispatcher("Member/MemberUpdate.jsp").forward(req, resp);
		}
	}
}
